﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Forms;

namespace eTMS.UserControls
{
    public partial class LoaiGiayToCheckList : Controls.UserControlBase
    {
        public LoaiGiayToCheckList()
        {
            InitializeComponent();
        }
        private LoaiGiayToController controller;
        private void LoadLanguage()
        {
            ColumnTenLoaiGiayTo.HeaderText = LoaiGiayToStringMapping.TenLoaiGiayTo;
        }
        public void ClearChecked()
        {
            Commons.Common.FillTableColumnValue(tblData,"CHECKED",false);
        }
        private List<int> lsInvisible = new List<int>();
        public List<int> ListLoaiGiayToInvisibled
        {
            get
            {
                return lsInvisible;
            }
            set
            {
                lsInvisible = value;
            }
        }
        private List<int> _lsVisible = new List<int>();
        public List<int> ListLoaiGiayToVisibled
        {
            get
            {
                return _lsVisible;
            }
            set
            {
                _lsVisible = value;
            }
        }
        public void AddColumnSoLuong()
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            column.HeaderText = "Số lượng";
            column.Name = "ColSoLuong";
            column.DataPropertyName = "SoLuong";
            column.Width = 80;
            gridData.Columns.Add(column);
        }
        public void SetSoLuongLoaiGiayTo(int LoaiGiayTo_ID, int SoLuong)
        {
            DataRow[] rows = controller.Table.Select("LoaiGiayTo_ID=" + LoaiGiayTo_ID.ToString());
            foreach (DataRow row in rows)
            {
                row["SoLuong"] = SoLuong;
            }
        }
        public void AddColumnGhiChu()
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            column.HeaderText = "Ghi chú";
            column.Name = "ColGhiChu";
            column.DataPropertyName = "GhiChu";
            column.Width = 200;
            gridData.Columns.Add(column);
        }
        public List<int> ListCheckedLoaiGiayTo
        {
            get
            {
                List<int> ls = new List<int>();
                foreach (DataRowView view in controller.Table.DefaultView)
                {
                    if ((bool)view["CHECKED"])
                    {
                        ls.Add(controller.ConvertInt(view["LOAIGIAYTO_ID"]));
                    }
                }
                return ls;
            }
        }
        private DataTable tblData=new DataTable();
        public void BindData()
        {
            if (controller == null)
                controller = new LoaiGiayToController();
            tblData = controller.Table;
            if (!tblData.Columns.Contains("SoLuong"))
            {
                tblData.Columns.Add("SoLuong", typeof(int));
                tblData.Columns.Add("GhiChu", typeof(string));
            }
            if (lsInvisible.Count > 0)
                tblData.DefaultView.RowFilter = "LOAIGIAYTO_ID NOT IN(" + string.Join(",", lsInvisible.ConvertAll<string>(Convert.ToString).ToArray()) + ")";
            if (_lsVisible.Count > 0)
            {
                string fitler = tblData.DefaultView.RowFilter;
                if (fitler.Length > 0)
                    fitler += " AND ";
                fitler += "LOAIGIAYTO_ID IN(" + string.Join(",", _lsVisible.ConvertAll<string>(Convert.ToString).ToArray()) + ")";
            }
            if (!tblData.Columns.Contains("CHECKED"))
                Commons.Common.AddCheckedToTable(tblData);
            gridData.AutoGenerateColumns = false;
            gridData.DataSource = tblData;
        }
        public DataTable GetData()
        {
            return tblData;
        }
        private void LoaiGiayToCheckList_Load(object sender, EventArgs e)
        {
            LoadLanguage();
            if (!DesignMode)
            {
                BindData();
            }
        }

        private void cbkAll_CheckedChanged(object sender, EventArgs e)
        {
            eTMS.Commons.Common.FillTableColumnValue(controller.Table, "CHECKED", cbkAll.Checked);
            gridData.Update();
        }
    }
}
